<div class="dialog" id="loginBox">
    <div class="dialog-content" style="width:400px; margin: 0 0 0 -225px">
        <div class="dialog-tit clearfix">
            <strong>登录</strong>
            <a href="#" class="close error-icon">×</a>
        </div>
        <div class="content" >
            <form action="" id="formLogin" >
                <div class="dg-list-item">
                    <span class="label-txt">手机号码</span>
                    <input type="text" name="phone" class="label-ipt" />
                </div>
                <div class="dg-list-item">
                    <span class="label-txt">密码</span>
                    <input type="password" name="pwd" class="label-ipt" />
                </div>

                <div class="dg-list-item">
                    <span class="label-txt"></span>
                    <input type="button" value="立即登录" class="red-link" id="J_submitLogin"/>
                </div>
                <div class="dg-list-item">
                    <span class="label-txt"></span>
                    <span class="reg-link">没有账号？<a href="javascript:void(0)" onclick="dialogShow('reg')">立即注册</a></span>
                </div>
            </form>
        </div>
    </div>
</div>

<div class="dialog" id="regBox">
    <div class="dialog-content" style="width:400px; margin: 0 0 0 -225px">
        <div class="dialog-tit clearfix">
            <strong>免费注册</strong>
            <a href="#" class="close error-icon">×</a>
        </div>
        <div class="content">
            <form action="" id="formReg">
                <div class="dg-list-item">
                    <span class="label-txt">手机号码</span>
                    <input type="text" name="phone" value="" class="label-ipt" />
                </div>
                <div class="dg-list-item">
                    <span class="label-txt">短信码</span>
                    <input type="text" name="code" class="label-ipt" style="width:80px" />
                    <a href="javascript:void(0)" class="getCode" id="J_getCode">获取验证码</a>

                </div>
                <div class="dg-list-item">
                    <span class="label-txt">密码</span>
                    <input type="password" name="pwd" class="label-ipt" />
                </div>
                <div class="dg-list-item">
                    <span class="label-txt">确认密码</span>
                    <input type="password" name="conpwd" class="label-ipt" />
                </div>

                <div class="dg-list-item">
                    <span class="label-txt"></span>
                    <input type="button" name="dosubmit" value="免费注册" class="red-link" id="J_submitReg" />
                </div>
                <div class="dg-list-item">
                    <span class="label-txt"></span>
                    <span class="reg-link">已有账号？<a href="javascript:void(0)" onclick="dialogShow('login')">立即登录</a></span>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
    (function(){

        $(".dialog").find(".close").on("click",function(){
            $(this).parents(".dialog").hide();
        })

        $("#J_submitLogin").on("click",function(){
            var form=$("#formLogin"),
                phone=form.find("input[name='phone']").val(),
                pwd=form.find("input[name='pwd']").val();

            if(!/^(13|15|18|14|17)[0-9]{9}$/.test(phone)||!/^\w{6,20}$/.test(pwd)){
                alert("手机号或密码格式有误！");
                return false;
            }

            var data={"username":phone,"password":pwd},
                url="/Index/login.html";
            $.ajax({
                url:url,
                type:"POST",
                data:data,
                datType:"JSON",
                success:function(res){
                    if(res.status==1){
                        window.history.go(0);
                    }else{
                        alert(res.info);
                    }
                }
            })
        })

        $("#formLogin").on("keyup",function(e){
            if(e.keyCode==13){
                
                return false;
            }
        })


        $("#J_getCode").on("click",function(){
            var _t = $(this);

            if (_t.hasClass("getCodeGray")) {
                return false;
            }

            var form=$("#formReg"),
                phone=form.find("input[name='phone']").val();
            if(!/^(13|15|18|14|17)[0-9]{9}$/.test(phone)){
                alert("请输入正确的手机号");
                return false;
            }
            _t.addClass("getCodeGray");

            $.ajax({
                url:"/Index/regVal.html",
                type:"POST",
                data:{"username":phone},
                datType:"JSON",
                success:function(res){
                    if(res.status==1){
                        setCookie("codeMin",60);
                        countDown(_t);
                    }else{
                        _t.removeClass("getCodeGray");
                        alert(res['info']);
                    }
                }
            })
        })
        getCodeStatus();
        function getCodeStatus(){
            var min=getCookie("codeMin");
            if(min>0){
                $("#J_getCode").addClass("getCodeGray");
                countDown($("#J_getCode"));
            }
        }

        //验证码倒计时
        function countDown(_t) {

            min=getCookie("codeMin")*1-1;

            if (min == 0) {
                _t.html("获取验证码");
                _t.removeClass("getCodeGray");
            } else {
                _t.html(min+"秒后重新获取");
                setCookie("codeMin",min--);
                setTimeout(function() {
                    countDown(_t);
                },1000)
            }
        }
        
        $("#J_submitReg").on("click",function(){
            var form=$("#formReg"),
                phone=form.find("input[name='phone']").val(),
                pwd=form.find("input[name='pwd']").val(),
                conPwd=form.find("input[name='conpwd']").val(),
                code=form.find("input[name='code']").val();

            if(!/^(13|15|18|14|17)[0-9]{9}$/.test(phone)||!/^\w{6,20}$/.test(pwd)){
                alert("帐号或密码格式有误！");
                return false;
            }else if(!/\d{6}/.test(code)){
                alert("请输入短信验证码！");
                return false;
            }else if(conPwd!=pwd){
                alert("二次输入的密码不一致！");
                return false;
            }

            var data={"username":phone,"password":pwd,"conpwd":pwd,"code":code},
                url="/Index/reg.html";
            $.ajax({
                url:url,
                type:"POST",
                data:data,
                datType:"JSON",
                success:function(res){
                    alert(res['info']);
                    if(res.status==1){
                        window.history.go(0);
                    }

                }
            })

        })
    })()
    
    function dialogShow(type){
        if(type=="login"){
            $("#loginBox").show();
            $("#regBox").hide();
        }else{
            $("#regBox").show();
            $("#loginBox").hide();
        }
    }
</script>